package pro.shushi.pamirs.demo.core.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import pro.shushi.pamirs.demo.api.enumeration.OrderStatusEnum;
import pro.shushi.pamirs.demo.api.model.Order;
import pro.shushi.pamirs.demo.api.service.OrderService;
import pro.shushi.pamirs.meta.annotation.Action;
import pro.shushi.pamirs.meta.annotation.Function;
import pro.shushi.pamirs.meta.annotation.Model;
import pro.shushi.pamirs.meta.api.session.PamirsSession;
import pro.shushi.pamirs.meta.constant.FunctionConstants;
import pro.shushi.pamirs.meta.enmu.ActionContextTypeEnum;
import pro.shushi.pamirs.meta.enmu.ViewTypeEnum;

import java.util.Date;


@Component
@Model.model(Order.MODEL_MODEL)
public class OrderAction {


    @Autowired
    private OrderService orderService;

    @Action.Advanced(name = FunctionConstants.create, managed = true)
    @Action(displayName = "创建", summary = "创建", bindingType = ViewTypeEnum.FORM)
    @Function(name = FunctionConstants.create)
    @Function.fun(FunctionConstants.create)
    public Order create(Order data) {
        return orderService.create(data);
    }

    @Action(displayName = "取消",bindingType = ViewTypeEnum.TABLE,contextType = ActionContextTypeEnum.SINGLE)
    @Action.Advanced(invisible = "!(activeRecord.status == 'un_confirm')")
    public Order itemInActive(Order data) {
        Order update = new Order();
        update.setId(data.getId());
        update.setStatus(OrderStatusEnum.cancel);
        update.setCancelReason(data.getCancelReason());
        update.updateById();
        return data;
    }


    @Action(displayName = "确认",bindingType = ViewTypeEnum.TABLE,contextType = ActionContextTypeEnum.SINGLE)
    @Action.Advanced(invisible = "!(activeRecord.status == 'un_confirm')")
    public Order confirm(Order data) {
        Order update = new Order();
        update.setId(data.getId());
        update.setConfirmFlag(data.getConfirmFlag());
        update.setConfirmTime(new Date());
        update.setConfirmUser(PamirsSession.getUserId());
        update.setConfirmRemark(data.getConfirmRemark());
        update.setStatus(OrderStatusEnum.unpay);
        update.updateById();
        return data;
    }

}
